<?php
namespace App\Http\Controllers;

use App\Models\Noteinfo;
use App\Models\Spot;
use App\Models\Travelnote;
use Illuminate\Support\Facades\Schema;

class TravelnoteController extends WxBaseController{
    protected $except=['index','show','search','info'];
    public function index($num=10){
        $notes = Travelnote::where('display',1)->orderBy('created_at','desc')->select('id','title','is_admin','recommended','coverimage','star','author','area','spot','created_at')->simplePaginate($num);
        foreach ($notes as $val){
            if($val->is_admin==0){
                $val->coverimage = config('domain').'/'.$val->coverimage;
            }
        }
        return $this->apiReturn(true,'success',$notes);
    }
    public function info(){
        $infos = Noteinfo::get();
        editImageUrl($infos);
        return $this->apiReturn(true,'success',$infos);
    }
    public function show($id){
        $note = Travelnote::where('id',$id);
        //更新浏览量
        if(Schema::hasColumn('travelnotes', 'watch')){
            $note->increment('watch');
        };
        $arr=$note->get();
        if($arr[0]->is_admin==0){
            $arr[0]->coverimage = config('domain').'/'.$arr[0]->coverimage;
        }
        return $this->apiReturn(true,'success',$arr[0]);
    }
    public function search($input){
        $notes=Travelnote::where('title', 'like', '%'.$input.'%')->get();
        foreach ($notes as $val){
            if($val->is_admin==0){
                $val->coverimage = config('domain').'/'.$val->coverimage;
            }
        }
        return $this->apiReturn(true,'success',$notes);
    }
    //写游记
    public function store(){
        $input = request();
        $id=$input->id;
        $spot=Spot::find($id);
        $params =[
            'title'=>$input->title,
            'content'=>$input->con,
            'coverimage'=>$input->pic,
            'area'=>$spot->region->text,
            'spot'=>$spot->scene,
            'avatar'=>$this->auth()->avatarUrl,
            'author'=>$this->auth()->nickname,
            'wxuser_id'=>$this->auth()->id,
            'created_at'=>time(),
            'updated_at'=>time()
        ];
        $id = Travelnote::insertGetId($params);
        return $this->apiReturn(true,'success',Travelnote::find($id));
    }
}